package com.zxq.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxq.exchange.enums.EFrame;
import com.zxq.exchange.model.ExchangeDTO;
import com.zxq.web.dto.PlatformCmdDTO;
import com.zxq.web.iot.domain.IotCmd;
import com.zxq.web.iot.domain.IotDevice;
import com.zxq.web.iot.service.impl.*;
import com.zxq.web.service.ITransformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class ITransformServiceImpl implements ITransformService {

    /**
     * 设备指令解码
     */
    @Autowired
    IotCmdDecodeServiceImpl iotCmdDecodeDoService;

    /**
     * 平台指令编码
     */
    @Autowired
    IotCmdEncoderServiceImpl iotCmdEncoderDoService;

    /**
     * 设备服务
     */
    @Autowired
    IotDeviceServiceImpl iotDeviceDoService;

    /**
     * 产品服务
     */
    @Autowired
    IotProjectServiceImpl iotProjectDoService;

    /**
     * cmd服务
     */
    @Autowired
    IotCmdServiceImpl cmdDoService;

    /**
     * 寻找包内所有字段
     */
    @Autowired
    IotPacketFieldServiceImpl iotPacketFieldDoService;


    @Autowired
    ITransformCmdServiceImpl transformCmdService;



    @Override
    public ExchangeDTO platTransform(PlatformCmdDTO platformCmdDTO) {

        String cmdHex = cmdDoService.selectIotCmdById(platformCmdDTO.getCmdId()).getCmdHex();
        byte[] total = transformCmdService.platformTransform(platformCmdDTO.getCmdId(),platformCmdDTO.getJson());
        ExchangeDTO exchangeDTO = new ExchangeDTO();
        exchangeDTO.setFrame(EFrame.getType(platformCmdDTO.getFrameId()));
        exchangeDTO.setData(total);
        exchangeDTO.setCmdHex(cmdHex);
        exchangeDTO.setDevId(platformCmdDTO.getDevId());

        return exchangeDTO;
    }




    @Override
    public PlatformCmdDTO devTransform(ExchangeDTO exchangeDTO) {
        String devId = exchangeDTO.getDevId();

        IotDevice iotDeviceCondition = new IotDevice();
        iotDeviceCondition.setDevId(devId);
        List<IotDevice> iotDeviceDos = iotDeviceDoService.selectIotDeviceList(iotDeviceCondition);
        if(iotDeviceDos.size() == .0) {
            log.info("not find device:" + exchangeDTO.getDevId());
            return null;
        }
        IotDevice iotDeviceDo = iotDeviceDos.get(0);
        Long projectId = iotDeviceDo.getProjectId();
        Long packetId = iotProjectDoService.selectIotProjectById(projectId).getPacketId();

        String cmdHex =  exchangeDTO.getCmdHex();
        IotCmd iotCmdCondition = new IotCmd();
        iotCmdCondition.setPacketId(packetId);
        iotCmdCondition.setCmdHex(cmdHex);
        List<IotCmd> iotCmdDos = cmdDoService.selectIotCmdList(iotCmdCondition);
        if(iotCmdDos.size() == 0) {
            log.info("not find cmdHex:" + cmdHex + " in device:" + devId);
            return null;
        }
        IotCmd iotCmd = iotCmdDos.get(0);
        Long cmd_id =  iotCmd.getId();
        String jsonStr = transformCmdService.deviceTransform(cmd_id,exchangeDTO.getData());

        PlatformCmdDTO platformCmdDTO = new PlatformCmdDTO();
        platformCmdDTO.setCmdId(iotCmd.getId());
        platformCmdDTO.setDevId(devId);
        platformCmdDTO.setFrameId(exchangeDTO.getFrame().getValue());
        platformCmdDTO.setJson(jsonStr);

        return platformCmdDTO;
    }




}
